package com.yxsd.web.controller.admin;

import com.pandawork.core.exception.SSException;
import com.yxsd.common.annotation.IgnoreLogin;
import com.yxsd.common.entity.Admin;
import com.yxsd.common.utils.WebConstants;
import com.yxsd.web.spring.AbstractController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * AdminController
 *
 * @author: zhangteng
 * @time: 2015/1/4 18:46
 */
@Controller
@RequestMapping("admin")
public class AdminController extends AbstractController {

    @RequestMapping(value = {"", "index"}, method = RequestMethod.GET)
    public String index() {
        return "admin/index";
    }

    @IgnoreLogin
    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String toLogin() {
        return "admin/login";
    }
    @IgnoreLogin
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String login(Admin admin, Model model) {
        try {
            admin = adminService.login(admin);
            if (admin != null) {
                getRequest().getSession().setAttribute(WebConstants.SESSIONUID, admin.getId());
                getRequest().getSession().setAttribute(WebConstants.SESSIONUNAME, admin.getUsername());
                return "admin/index";
            } else {
                model.addAttribute("msg", "用户名或密码错误!");
                return "admin/login";
            }
        } catch (SSException e) {
            sendErrMsg(e.getMessage());
            return WebConstants.sysErrorCode;
        }
    }

}
